from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
# from rest_framework import serializers
from . import serializers
# from .serializers import *
# Create your views here.
class Register(APIView):
    def post(self,request):
        username = request.data['username']
        password = request.data['password']
        password2 = request.data['password2']
        
        img = request.FILES['file']
        print(img)
        if not all([username,password,password2]):
            data = {
                'code':201,
                'message':'数据不完善'
            }
        else:
            if Uesr.objects.filter(username=username).first():
                data = {
                    'code':202,
                    'message':'账号重复'
                }
            else:
                Uesr.objects.create(username=username,password=password,img=img)
                data={
                    'code':200,
                    'message':'注册成功'
                }
        return Response(data)

class Login(APIView):
    def post(self,request):
        username = request.data['username']
        password = request.data['password']
        user = Uesr.objects.filter(username=username,password=password).first()
        if not all([username,password]):
            data = {
                'code':201,
                'message':'输入不完整'
            }
        else:
            if not user:
                data = {
                    'code':202,
                    'message':'账号密码错误'
                }
            else:
                user.count = user.count+1
                user.save()
                data = {
                    'code':200,
                    'message':'登陆成功',
                    'user':username
                }
        return Response(data)

class Index(APIView):
    def get(self,request):
        user=Uesr.objects.all()
        users=serializers.user_serializers(instance=user,many=True)
        return Response({
            'code':200,
            'users':users.data
        })

class Delete(APIView):
    def post(self,request):
        id = request.data['id_']
        Uesr.objects.get(pk=id).delete()
        return Response({
            'code':200,
            'message':'删除成功'
        })

class Update(APIView):
    def post(self,request):
        id=request.data['id_']
        username = request.data['username']
        if not all([username]):
            return Response({
                'code':201,
                'message':'输入不完整'
            })


        user = Uesr.objects.get(pk=id)
        user.username = username
        user.save()
        return Response({
            'code':200,
            'message':'成功'
        })

from django.core import paginator

class Showgood(APIView):
    def post(self, request):
        c = Uesr.objects.all()
        try:
            p = int(request.data['p'])
            # print(p)
        except:
            p = 1
        tiao = paginator.Paginator(c,3)
        pa = tiao.page(p)
        cc = serializers.user_serializers(instance=pa, many=True)

        return Response({
            'code': 200,
            'message': cc.data,
            'list':list(tiao.page_range)
        })

class Counts(APIView):
    def get(self,request):

        counter = cishu.objects.get(pk=1) ##cishu表名称
        counter.counts +=1  ##counts字段
        counter.save()
        # aa = counter.counts
        return  Response({
            'code':200,
            'up':counter.counts
        })












